<p>
  We start by creating a scheduled event, <code>MonthEnd()</code>, that will trigger the algorithm to buy SPY.
</p>
<div class="section-example-container">
<pre class="python">self.Schedule.On(
    self.DateRules.MonthEnd(self.spy),
    self.TimeRules.AfterMarketOpen(self.spy, 1),
    self.Purchase)
</pre>
</div>
<p>
  We will purchase the SPY immediately, and we will wait 3 trading days, as suggested, before liquidating our portfolio. The boolean <code>self.Portfolio.Invested</code> will help us wait 3 days before executing the liquidate order in <code>OnData()</code>. The equity index is bought and sold every month.
</p>
<div class="section-example-container">
<pre class="python">def Purchase(self):
    ''' Immediately purchases the ETF at market opening '''
    self.SetHoldings(self.spy, 1)
    self.days = 0

def OnData(self, data):
    if self.Portfolio.Invested:
        self.days += 1

        # Liquidates after 3 days
        if self.days > 3:
            self.Liquidate(self.spy, 'Liquidate after 3 days')
</pre>
</div>